asktopia asktopia logo
Questions home
Office desktop

Lextree, data and documents in one place.

  • c program to Replace a word in a text by another given word

    bookmark_border 0

    I need help with an exercise in C. This is what I tried to do, but it doesn't work.
     
    int main()
    {
        int i, leng;
        char phrase[DIM], word[DIM], word2[DIM];
    
        printf("Write a sentence\n>>");
        fgets(phrase, sizeof(phrase), stdin);
    
        printf("Enter the word you want to replace in the sentence\n>> ");
        fgets(word, sizeof(word), stdin);
    
        printf("What do you want to replace it with?\n>> ");
        fgets(word2, sizeof(word2), stdin);
    
        leng = strlen(phrase);
    
        for(i = 0; i < leng; i++){
            if(phrase[i] == word){
                phrase[i] = word2;
            }
        }
        printf("%s", phrase);

     When I start it, these warnings appear:
     
     assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
     
     comparison between pointer and integer
     
     What I have tried:
     
    I tried to use strcmp, but i don't know how to use it with this exercise 

    cprogramming

    asked , updated

    by pippepappe

  • Answers

  • answered , updated

    by kamal97

    bookmark_border 0

    Check out the answers to this same question on Stack Overflow: https://stackoverflow.com/questions/15465755/replacing-a-word-in-a-text-by-another-given-word

About
Terms
Privacy

© 2022 asktopia. All rights reserved.